home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_src.lha / KUSER / KINIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-31  |  4.5 KB  |  205 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/kuser/RCS/kinit.c,v $
  3.  * $Author: jon $ 
  4.  *
  5.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology. 
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>. 
  9.  *
  10.  * Routine to initialize user to Kerberos.  Prompts optionally for
  11.  * user, instance and realm.  Authenticates user and gets a ticket
  12.  * for the Kerberos ticket-granting service for future use. 
  13.  *
  14.  * Options are: 
  15.  *
  16.  *   -i[instance]
  17.  *   -r[realm]
  18.  *   -v[erbose]
  19.  *   -l[ifetime]
  20.  */
  21.  
  22. #ifndef    lint
  23. static char rcsid_kinit_c[] =
  24. "$Id: kinit.c,v 4.12 90/03/20 16:11:15 jon Exp $";
  25. #endif    lint
  26.  
  27. #include <conf.h>
  28. #include <mit_copy.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. /* #include <pwd.h> */
  32. #include <krb.h>
  33.  
  34. #ifndef ORGANIZATION
  35. #define ORGANIZATION "MIT Project Athena"
  36. #endif /*ORGANIZATION*/
  37.  
  38. #ifdef    PC
  39. #define    LEN    64        /* just guessing */
  40. #ifdef __BORLANDC__
  41. int _stklen=16000;
  42. #endif
  43. #endif    PC
  44.  
  45. #ifdef    BSD42
  46. #include <string.h>
  47. /* #include <sys/param.h> */
  48. #if     defined(ultrix) || defined(sun)
  49. #define LEN    64
  50. #else
  51. #define    LEN    64
  52. #endif    /* defined(ultrix) || defined(sun) */
  53. #endif    /* BSD42 */
  54.  
  55. #define    LIFE    96    /* lifetime of ticket in 5-minute units */
  56.  
  57. char   *progname;
  58. extern int krb_debug,krb_ap_req_debug;
  59.  
  60. void
  61. get_input(s, size, stream)
  62. char *s;
  63. int size;
  64. FILE *stream;
  65. {
  66.     char *p;
  67.  
  68.     if (fgets(s, size, stream) == NULL)
  69.       exit(1);
  70.     if ( (p = (char*)index(s, '\n')) != NULL)
  71.         *p = '\0';
  72. }
  73.  
  74. main(argc, argv)
  75.     char   *argv[];
  76. {
  77.     char    aname[ANAME_SZ];
  78.     char    inst[INST_SZ];
  79.     char    realm[REALM_SZ];
  80.     char    buf[LEN];
  81.     char   *username = NULL;
  82.     int     iflag, rflag, vflag, lflag, lifetime, k_errno;
  83.     register char *cp;
  84.     register i;
  85.  
  86.     krb_debug=1;
  87.     krb_ap_req_debug=1;
  88.     
  89.     *inst = *realm = '\0';
  90.     iflag = rflag = vflag = lflag = 0;
  91.     lifetime = LIFE;
  92.     progname = (cp = rindex(*argv, '\\')) ? cp + 1 : *argv;
  93.  
  94.     while (--argc) {
  95.     if ((*++argv)[0] != '-') {
  96.         if (username)
  97.         usage();
  98.         username = *argv;
  99.         continue;
  100.     }
  101.     for (i = 1; (*argv)[i] != '\0'; i++)
  102.         switch ((*argv)[i]) {
  103.         case 'i':        /* Instance */
  104.         ++iflag;
  105.         continue;
  106.         case 'r':        /* Realm */
  107.         ++rflag;
  108.         continue;
  109.         case 'v':        /* Verbose */
  110.         ++vflag;
  111.         continue;
  112.         case 'l':
  113.         ++lflag;
  114.         continue;
  115.         default:
  116.         usage();
  117.         exit(1);
  118.         }
  119.     }
  120.     if (username &&
  121.     (k_errno = kname_parse(aname, inst, realm, username))
  122.     != KSUCCESS) {
  123.     fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
  124.     iflag = rflag = 1;
  125.     username = NULL;
  126.     }
  127. #if 0
  128.     if (k_gethostname(buf, LEN)) {
  129.     fprintf(stderr, "%s: k_gethostname failed\n", progname);
  130.     exit(1);
  131.     }
  132.     printf("%s (%s)\n", ORGANIZATION, buf);
  133. #endif    
  134.     if (username) {
  135.     printf("Kerberos Initialization for \"%s", aname);
  136.     if (*inst)
  137.         printf(".%s", inst);
  138.     if (*realm)
  139.         printf("@%s", realm);
  140.     printf("\"\n");
  141.     } else {
  142.     printf("Kerberos Initialization\n");
  143.     printf("Kerberos name: ");
  144.     get_input(aname, sizeof(aname), stdin);
  145.     if (!*aname)
  146.         exit(0);
  147.     if (!k_isname(aname)) {
  148.         fprintf(stderr, "%s: bad Kerberos name format\n",
  149.             progname);
  150.         exit(1);
  151.     }
  152.     }
  153.     /* optional instance */
  154.     if (iflag) {
  155.     printf("Kerberos instance: ");
  156.     get_input(inst, sizeof(inst), stdin);
  157.     if (!k_isinst(inst)) {
  158.         fprintf(stderr, "%s: bad Kerberos instance format\n",
  159.             progname);
  160.         exit(1);
  161.     }
  162.     }
  163.     if (rflag) {
  164.     printf("Kerberos realm: ");
  165.     get_input(realm, sizeof(realm), stdin);
  166.     if (!k_isrealm(realm)) {
  167.         fprintf(stderr, "%s: bad Kerberos realm format\n",
  168.             progname);
  169.         exit(1);
  170.     }
  171.     }
  172.     if (lflag) {
  173.      printf("Kerberos ticket lifetime (minutes): ");
  174.      get_input(buf, sizeof(buf), stdin);
  175.      lifetime = atoi(buf);
  176.      if (lifetime < 5)
  177.           lifetime = 1;
  178.      else
  179.           lifetime /= 5;
  180.      /* This should be changed if the maximum ticket lifetime */
  181.      /* changes */
  182.      if (lifetime > 255)
  183.           lifetime = 255;
  184.     }
  185.     if (!*realm && krb_get_lrealm(realm, 1)) {
  186.     fprintf(stderr, "%s: krb_get_lrealm failed\n", progname);
  187.     exit(1);
  188.     }
  189.     k_errno = krb_get_pw_in_tkt(aname, inst, realm, "krbtgt", realm,
  190.                 lifetime, (char*)NULL);
  191.     if (vflag) {
  192.     printf("Kerberos realm %s:\n", realm);
  193.     printf("%s\n", krb_err_txt[k_errno]);
  194.     } else if (k_errno) {
  195.     fprintf(stderr, "%s: %s\n", progname, krb_err_txt[k_errno]);
  196.     exit(1);
  197.     }
  198. }
  199.  
  200. usage()
  201. {
  202.     fprintf(stderr, "Usage: %s [-irvl] [name]\n", progname);
  203.     exit(1);
  204. }
  205.